﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AForge.Imaging;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using AForge.Math;
using AForge;
using System.Drawing.Imaging;
using AForge.Math.Geometry;

namespace Analizador_de_Semillas
{
    class Analizador
    {
        public static void CargarHistograma(PictureBox pic, Chart chart,String canal)
        {

            Bitmap imagen = new Bitmap(pic.Image);
            
            ImageStatistics rgbStatistics = new ImageStatistics(imagen);

            Histogram histo = null;
            
            switch (canal)
            {
                case "Rojo": histo = new Histogram(rgbStatistics.Red.Values);
                    break;

                case "Azul": histo = new Histogram(rgbStatistics.Blue.Values);
                    break;

                case "Verde": histo = new Histogram(rgbStatistics.Green.Values);
                    break;
                
            }
            
            Series datos = new Series();

            //chart.ChartAreas[0].AxisY.Interval = histo.Max;

            for (int i = 0; i <= 255; i++)
            {
                datos.Points.Add(histo.Values[i]);
            }

            chart.Series.Add(datos);

        }

        private static int[] SmoothHistogram(int[] originalValues)
        {
            int[] smoothedValues = new int[originalValues.Length];

            double[] mask = new double[] { 0.25, 0.5, 0.25 };

            for (int bin = 1; bin < originalValues.Length - 1; bin++)
            {
                double smoothedValue = 0;
                for (int i = 0; i < mask.Length; i++)
                {
                    smoothedValue += originalValues[bin - 1 + i] * mask[i];
                }
                smoothedValues[bin] = (int)smoothedValue;
            }

            return smoothedValues;
        }

        public static ImageStatistics StatisticsImage(PictureBox pic) 
        {
            Bitmap img = new Bitmap(pic.Image);

            ImageStatistics statistics = new ImageStatistics(img);

            return statistics;
        }

        public static int ContarObjetos(PictureBox pic)
        {
            Bitmap img = new Bitmap(pic.Image);
            
            AForge.Imaging.Filters.Grayscale fil = new AForge.Imaging.Filters.Grayscale(0.30, 0.59, 0.11);
            
            Bitmap imgF = fil.Apply(img);
            
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.MinWidth = 10;

            blobCounter.MinHeight = 10;

            blobCounter.FilterBlobs = true;

            blobCounter.ProcessImage(imgF);

            Rectangle[] rects = blobCounter.GetObjectsRectangles();

            foreach (Rectangle recs in rects)

                if (rects.Length > 0)
                {
                    foreach (Rectangle objectRect in rects)
                    {

                        Graphics g = Graphics.FromImage(pic.Image);

                        using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
                        {
                            g.DrawRectangle(pen, objectRect);
                        }

                        g.Dispose();
                    }

                }

            return rects.Length;
        }

    }
}
